#Makefile which compiles the C++ portion(s) of GAIA.
##########################################################################
#Copyright 2010 Bryan Killett
#
#This file is part of GAIA.
#
#GAIA is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#GAIA is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with GAIA. If not, see <http://www.gnu.org/licenses/>.
##########################################################################
#Definitions section.
##########################################################################

#Use if GSL ISN'T installed- **** CHANGE definitions.cpp ALSO!
LFLAGS=-lfes -lm
#Use if GSL IS installed.
#LFLAGS=-lfes -lgsl -lgslcblas -lm

#Dir. that has FES routines relative to GAIAFOLDER/c
#This is a simply-expanded variable so /src can be appended in a recipe.
FES_DIR := ../data/FES2004
IFLAGS=-I$(FES_DIR)/include -I$(FES_DIR)/src -L$(FES_DIR)/lib

#Dir. that has PROJ4 routines relative to GAIAFOLDER/c
PROJ4_DIR = ../software/proj-4.7.0

#Designed to make editing the compilation statement easier.
L1_FILES=definitions.hpp double_constants.hpp
L2_FILES=functions.o
COMPILER=g++

#The following flags are used for development and generate a lot of output.
#Flag -Winline produces lots of bizarre warnings, so it's not used.
#-O2 slows compile, removes values from gdb, so it's frequently deleted.
#Also, -static and -pedantic screw up Iliff, so iliff_Makefile should be 
#an exact copy of this file (keep it updated!) but without those flags and
#with -O2 (make sure it's there, because it's always used on fast PCs).
#If profiling is desired, -pg, run program, then gprof -l it.
#MFLAGS= -g -static -pedantic -Wall -W -Wshadow -Wunused -Wextra -Wredundant-decls -Wmissing-declarations -Wmissing-noreturn -Wundef -Wno-long-long

#Default flags for quick installation and test drive:
MFLAGS= -Wno-long-long

##########################################################################
#Rules and dependencies section.
##########################################################################

#This rule is first so that 'make' goes here by default.  It compiles the
#original gaia.cpp in 2 levels.  The 1st level creates
#gaia.o, which is time consuming but hopefully rare.  The 2nd level links
#gaia.o to gaia.cpp.  This line is similar to all the other linking statements.
main: $(L2_FILES) gaia.cpp
	$(COMPILER) gaia.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o gaia.out

#This is the level 1 compile statement.  It takes a long time, and should only
#rarely be necessary.
functions.o: $(L1_FILES) functions.cpp
	cd $(FES_DIR)/src && $(MAKE)
	$(COMPILER) -c functions.cpp $(MFLAGS) $(IFLAGS)

#modify_maps.
modify_maps: $(L2_FILES) modify_maps.cpp
	$(COMPILER) modify_maps.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o modify_maps.out

#modify_plots.
modify_plots: $(L2_FILES) modify_plots.cpp
	$(COMPILER) modify_plots.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o modify_plots.out

#Program that examines input file.
examine_input: $(L2_FILES) examine_input.cpp
	$(COMPILER) examine_input.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o examine_input.out
	
#Program that modifies input file.
modify_input: $(L2_FILES) modify_input.cpp
	$(COMPILER) modify_input.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o modify_input.out
	
#Program to create simulated accels using perform_upward_continuation.
sim_accels: $(L2_FILES) sim_accels.cpp
	$(COMPILER) sim_accels.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o sim_accels.out
	
#fes_tidal_periods
fes_tidal_periods: $(L2_FILES) fes_tidal_periods.cpp
	$(COMPILER) fes_tidal_periods.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o ftp.out

#modify_fes
modify_fes: $(L2_FILES) modify_fes.cpp
	$(COMPILER) modify_fes.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o modify_fes.out

#interpolate_fes
interpolate_fes: $(L2_FILES) interpolate_fes.cpp
	$(COMPILER) interpolate_fes.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o interpolate_fes.out

#grace_stack_timeseries THIS ANALYZES GRACE INPUT TIMESERIES!
gst: $(L2_FILES) grace_stack_timeseries.cpp
	$(COMPILER) grace_stack_timeseries.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o gst.out

#analyze_timeseries THIS ANALYZES XY PLOTS, *NOT* GRACE INPUT TIMESERIES!
analyze_timeseries: $(L2_FILES) analyze_timeseries.cpp
	$(COMPILER) analyze_timeseries.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o analyze_timeseries.out

#Program that combines orbit data and accels.
combine: $(L2_FILES) combine_orbit_and_accels.cpp
	$(COMPILER) combine_orbit_and_accels.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o combine.out

#Program that developed the Greenwich TGP phase functions.
greenwich: $(L2_FILES) greenwich.cpp 
	$(COMPILER) greenwich.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o greenwich.out

#Test of GSL Fourier Transform routines- skips functions.o altogether.
gsl_fourier: gsl_fourier.cpp
	$(COMPILER) gsl_fourier.cpp $(MFLAGS) -lgsl -lgslcblas -lm -o gsl_fourier.out

#gsl_rng.
gsl_rng: $(L2_FILES) gsl_rng.cpp
	$(COMPILER) gsl_rng.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o gsl_rng.out

#developed statistical tests of the results after upward continuation.
upcontver: $(L2_FILES) upcontver.cpp 
	$(COMPILER) upcontver.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o upcontver.out
	
#Converts new (2009-12) stack format to old so gaia and convert can use it.
newstack_convert: $(L2_FILES) newstack_convert.cpp 
	$(COMPILER) newstack_convert.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o newstack_convert.out
	
#Solves a problem using various routines, makes sure answers are identical and compares performance.
solver_sandbox: $(L2_FILES) solver_sandbox.cpp
	$(COMPILER) solver_sandbox.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o solver_sandbox.out
	
#Converts from lat/lon/height to XYZ in the ECEF frame using various routines, makes sure answers are identical.
ellipsoid_sandbox: $(L2_FILES) ellipsoid_sandbox.cpp
	$(COMPILER) ellipsoid_sandbox.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o ellipsoid_sandbox.out

#Program that does whatever I want it to do that day.
testing: $(L2_FILES) testing.cpp 
	$(COMPILER) testing.cpp $(L2_FILES) $(MFLAGS) $(IFLAGS) $(LFLAGS) -o testing.out
	
#REMEMBER TO ADD NEW PROGRAMS TO "ALL" SO COMPILATION CAN BE TESTED EN MASSE!
all : main sim_accels modify_maps modify_plots examine_input modify_input fes_tidal_periods combine greenwich gst analyze_timeseries modify_fes interpolate_fes gsl_fourier gsl_rng upcontver newstack_convert solver_sandbox testing

.PHONY : clean
clean : 
	rm -f *.o
	rm -f *.out
	cd $(FES_DIR)/src && $(MAKE) clean
